package com.cmc.cloud.cmclink.doc.service.impl.manifest;

import cn.hutool.core.collection.CollectionUtil;
import com.cmc.cloud.cmclink.doc.api.document.rep.manifestrep.manifestvslvoy.ManifestVslvoyCreateReqVO;
import com.cmc.cloud.cmclink.doc.api.document.rep.manifestrep.manifestvslvoy.ManifestVslvoyRespVO;
import com.cmc.cloud.cmclink.doc.api.document.rep.manifestrep.manifestvslvoy.ManifestVslvoyUpdateReqVO;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.ManifestVslvoyErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.ManifestVslvoyConvert;
import com.cmc.cloud.cmclink.doc.entity.ManifestVslvoyDO;
import com.cmc.cloud.cmclink.doc.mapper.ManifestVslvoyMapper;
import com.cmc.cloud.cmclink.doc.service.manifest.ManifestVslvoyService;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 舱单航次路径 Service 实现类
 *
 * @author 集运管理员
 */
@Service
@Validated
public class ManifestVslvoyServiceImpl implements ManifestVslvoyService {

    @Resource
    private ManifestVslvoyMapper manifestVslvoyMapper;

    @Override
    public void createManifestVslvoy(ManifestVslvoyCreateReqVO createReqVO) {
        // 插入
        ManifestVslvoyDO manifestVslvoy = ManifestVslvoyConvert.INSTANCE.convert(createReqVO);
        manifestVslvoyMapper.insert(manifestVslvoy);
    }

    @Override
    public void updateManifestVslvoy(ManifestVslvoyUpdateReqVO updateReqVO) {
        // 校验存在
        validateManifestVslvoyExists(updateReqVO.getId());
        // 更新
        ManifestVslvoyDO updateObj = ManifestVslvoyConvert.INSTANCE.convert(updateReqVO);
        manifestVslvoyMapper.updateById(updateObj);
    }

    @Override
    public void deleteManifestVslvoy(Long id) {
        // 校验存在
        validateManifestVslvoyExists(id);
        // 删除
        manifestVslvoyMapper.deleteById(id);
    }

    private void validateManifestVslvoyExists(Long id) {
        if (manifestVslvoyMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(ManifestVslvoyErrorCodeConstants.MANIFEST_VSLVOY_NOT_EXISTS);
        }
    }

    @Override
    public ManifestVslvoyDO getManifestVslvoy(Long id) {
        return manifestVslvoyMapper.selectById(id);
    }

    @Override
    public List<ManifestVslvoyDO> getManifestVslvoyList(Collection<Long> ids) {
        return manifestVslvoyMapper.selectBatchIds(ids);
    }

    @Override
    public void createOrUpdateAndDeleteBatch(Long manifestId, List<ManifestVslvoyUpdateReqVO> newList, List<ManifestVslvoyRespVO> oldList) {
        // 无效数据直接返回
        if (CollectionUtil.isEmpty(newList) && CollectionUtil.isEmpty(oldList)) {
            return;
        }
        // 默认值
        if (CollectionUtil.isEmpty(newList)) {
            newList = new ArrayList<>();
        }
        if (CollectionUtil.isEmpty(oldList)) {
            oldList = new ArrayList<>();
        }
        List<ManifestVslvoyUpdateReqVO> createList = new ArrayList<>();
        List<ManifestVslvoyUpdateReqVO> updateList = new ArrayList<>();
        // 历史数据
        Set<Long> oldIds = oldList.stream().map(e -> e.getId()).collect(Collectors.toSet());
        newList.forEach(e -> {
            if (e.getId() != null) {
                if (oldIds.contains(e.getId())) {
                    // 正常更新
                    updateList.add(e);
                    oldIds.remove(e.getId()); // 剩下的数据需要删除
                } else {
                    // 数据更新非法
                    throw ServiceExceptionUtil.exception(ManifestVslvoyErrorCodeConstants.MANIFEST_VSLVOY_NOT_EXISTS);
                }
            } else {
                e.setManifestId(manifestId);
                createList.add(e);
            }
        });
        if (CollectionUtil.isNotEmpty(oldIds)) {
            // 没有匹配上的就是前端删除的
            manifestVslvoyMapper.deleteBatchIds(oldIds);
        }
        if (CollectionUtil.isNotEmpty(createList)) {
            List<ManifestVslvoyDO> manifestVslvoyCreateList = ManifestVslvoyConvert.INSTANCE.convertUpdateList(createList);
            manifestVslvoyMapper.insertBatch(manifestVslvoyCreateList);
        }
        if (CollectionUtil.isNotEmpty(updateList)) {
            List<ManifestVslvoyDO> manifestVslvoyUpdateList = ManifestVslvoyConvert.INSTANCE.convertUpdateList(updateList);
            manifestVslvoyMapper.updateBatch(manifestVslvoyUpdateList);
        }
    }
}
